﻿using System;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YJKInspectModelFunc.ModelLayer;

namespace YJKInspectModelFunc.ControlLayer
{
	public class BaseLoopMangerNoneSwitch
	{
		public BaseLoopMangerNoneSwitch(UIDocument inputUiDoc, IModelessFormNoneSwitch inputForm, IWin32Window inputParentHandle = null, PickObjectMethod inputPickMethod = null)
		{
			if (inputUiDoc == null || inputForm == null)
			{
				throw new System.ArgumentNullException();
			}
			this.m_uiDoc = inputUiDoc;
			this.m_Doc = this.m_uiDoc.Document;
			this.m_useForm = inputForm;
			this.m_useParentHandle = inputParentHandle;
			if (inputPickMethod == null)
			{
				this.m_usePickObjectMethod = new PickObjectMethod(this.PickObject);
			}
		}

		public virtual void DoLoopWork()
		{
			this.m_useForm.ShowForm(this.m_useParentHandle);
			for (;;)
			{
				this.m_useForm.ResetContinueBreak();
				try
				{
					object inputValue = this.m_usePickObjectMethod();
					this.DoAfterPick(inputValue);
					continue;
				}
				catch (Exception ex)
				{
					if (!(ex is Autodesk.Revit.Exceptions.OperationCanceledException) && !(ex is EscException))
					{
						throw ex;
					}
					bool ifContinue = this.m_useForm.IfContinue;
					bool ifBreak = this.m_useForm.IfBreak;
					if (ifContinue)
					{
						this.DoBeforeContinue();
						continue;
					}
					if (!ifBreak)
					{
						continue;
					}
					this.DoBeforeClose();
					if (this.m_useForm.IfManualEsc)
					{
						this.m_useForm.CloseForm();
					}
				}
				break;
			}
			this.DoAfterWhile();
		}

		protected virtual void DoAfterPick(object inputValue)
		{
		}

		protected virtual void DoAfterWhile()
		{
		}

		protected virtual void DoBeforeClose()
		{
		}

		protected virtual void DoBeforeContinue()
		{
		}

		private object PickObject()
		{
			return this.m_uiDoc.Selection.PickPoint();
		}

		private Document m_Doc;

		private UIDocument m_uiDoc;

		protected IModelessFormNoneSwitch m_useForm;

		protected IWin32Window m_useParentHandle;

		protected PickObjectMethod m_usePickObjectMethod;
	}
}
